#! /usr/bin/env python
import os
import sys
import atexit
import getopt
import inspect
import readline

sys.path.append('/data/xce')
sys.path.append('/data/xce/IcePy-3.3.0/python')
import traceback, Ice, time

class SlicePathAndFile:
  def __init__(self):
    self.test_slice = (" -I../UtilSlice/slice " 
                       " -I../OceSlice/slice --all "
                       " ../UtilSlice/slice/Util.ice "
                       " ../OceSlice/slice/Notify.ice ")
    self.dev_slice = (
      "-I/data/home/longfei.meng/work/main_project/trunk/UtilSlice/slice " 
      "-I/data/home/longfei.meng/work/main_project/trunk/OceSlice/slice --all " 
      "/data/home/longfei.meng/work/main_project/trunk/UtilSlice/slice/Util.ice "
      "/data/home/longfei.meng/work/main_project/trunk/OceSlice/slice/Notify.ice "
      )
    self.product_slice = ""

test_slice = (" -I../UtilSlice/slice " 
                       " -I../OceSlice/slice --all "
                       " ../UtilSlice/slice/Util.ice "
                       " ../OceSlice/slice/Notify.ice ")
dev_slice = (
      "-I/data/home/longfei.meng/work/main_project/trunk/UtilSlice/slice " 
      "-I/data/home/longfei.meng/work/main_project/trunk/OceSlice/slice --all " 
      "/data/home/longfei.meng/work/main_project/trunk/UtilSlice/slice/Util.ice "
      "/data/home/longfei.meng/work/main_project/trunk/OceSlice/slice/Notify.ice "
      )
 
#Ice.loadSlice(dev_slice)
Ice.loadSlice(test_slice)
#Ice.loadSlice(
#    "-I/data/home/longfei.meng/work/main_project/trunk/UtilSlice/slice " 
#    "-I/data/home/longfei.meng/work/main_project/trunk/OceSlice/slice --all " 
#    "/data/home/longfei.meng/work/main_project/trunk/UtilSlice/slice/Util.ice "
#    "/data/home/longfei.meng/work/main_project/trunk/OceSlice/slice/Notify.ice "
#    )
#
from xce.notify import *
#product_locator="--Ice.Default.Locator=Notify/Locator:tcp -h f0.xce.d.xiaonei.com -p 14400" 
test_locator="--Ice.Default.Locator=Notify/Locator:tcp -h f8.xce.d.xiaonei.com -p 14400" 
trace_flag = "--Ice.Trace.Network=1"

class Preload:
  def __init__(self, locator, uid_file_name, out_file_name, trace_flag):
    self.locator_ = locator
    self.trace_flag_ = trace_flag
    self.ic_ = Ice.initialize([locator, trace_flag])
    self.uid_file_name = uid_file_name
    self.log_file_name = out_file_name
    self.bigtype = 3

  def preload(self, service_num):
    if os.path.isfile(self.uid_file_name) == False:
      print "The file [%s] is not exist" % self.uid_file_name
      return -1
    uid_file = open(self.uid_file_name)
    out_file = open(self.log_file_name, "a")
    indexN_prx = []
    start_time = time.time()
    for i in range(0, service_num):
      serv_name = "M@NotifyIndexN" + str(i)
      print "service name:%s" % serv_name
      prx = NotifyIndexManagerNPrx.checkedCast(self.ic_.stringToProxy(serv_name))
      indexN_prx.append(prx)
    for uid_str in uid_file:
      uid = int(uid_str)
      proxy_id = uid % service_num
      #print "uid:%d proxy_id:%d" %(uid, proxy_id)
      try:
        count = indexN_prx[proxy_id].GetUndealCountByBigtype(uid, self.bigtype)
        out_file.write("uid:%d bigtype:%d count:%d\n" % (uid, self.bigtype, count))
      except:
        traceback.print_exc()
    out_file.write("\n--Elasped Time:%s" % (time.time() - start_time))
 
def main():
  try:
    opt_list, arg_list = getopt.getopt(sys.argv[1:], "s:i:o:tp")
  except getopt.GetoptError, err:
    print(str(err))
    usage()
    sys.exit('Invalid argument:%s' % (sys.argv, ))
  test_flag = True 
  ice_locator = ""
  uid_file_name = "preload_uid"
  out_file_name = "preload_indexN.log"
  for opt, arg in opt_list:
    if opt == "-s":
      Ice.loadSlice(test_slice)
      service_name = str(arg)
    elif opt == "-i":
      uid_file_name = str(arg)
    elif opt == "-o":
      out_file_name = str(arg)
    elif opt == "-t":
      test_flag = True
      ice_locator = test_locator
    elif opt == "-p":
      test_flag = False
      ice_locator = product_locator
    else:
      sys.exit('unhandled option')
  preload = Preload(ice_locator, uid_file_name, out_file_name, trace_flag)
  # 10 is service number, if change the service,don't forget fix it
  preload.preload(10)

if __name__ == "__main__":
  main()

